/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.media.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.media.entity.HkMediaNews;
import com.thinkgem.jeesite.modules.hk.media.service.HkMediaNewsService;

/**
 * 自媒体新闻Controller
 * @author 罗天文
 * @version 2018-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/media/hkMediaNews")
public class HkMediaNewsController extends BaseController {

	@Autowired
	private HkMediaNewsService hkMediaNewsService;
	
	@ModelAttribute
	public HkMediaNews get(@RequestParam(required=false) String id) {
		HkMediaNews entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkMediaNewsService.get(id);
		}
		if (entity == null){
			entity = new HkMediaNews();
		}
		return entity;
	}
	
	@RequiresPermissions("media:hkMediaNews:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkMediaNews hkMediaNews, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkMediaNews> page = hkMediaNewsService.findPage(new Page<HkMediaNews>(request, response), hkMediaNews); 
		model.addAttribute("page", page);
		return "hk/media/hkMediaNewsList";
	}

	@RequiresPermissions("media:hkMediaNews:view")
	@RequestMapping(value = "form")
	public String form(HkMediaNews hkMediaNews, Model model) {
		model.addAttribute("hkMediaNews", hkMediaNews);
		return "hk/media/hkMediaNewsForm";
	}

	@RequiresPermissions("media:hkMediaNews:edit")
	@RequestMapping(value = "save")
	public String save(HkMediaNews hkMediaNews, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkMediaNews)){
			return form(hkMediaNews, model);
		}
		hkMediaNewsService.save(hkMediaNews);
		addMessage(redirectAttributes, "保存自媒体新闻成功");
		return "redirect:"+Global.getAdminPath()+"/media/hkMediaNews/?repage";
	}
	
	@RequiresPermissions("media:hkMediaNews:edit")
	@RequestMapping(value = "delete")
	public String delete(HkMediaNews hkMediaNews, RedirectAttributes redirectAttributes) {
		hkMediaNewsService.delete(hkMediaNews);
		addMessage(redirectAttributes, "删除自媒体新闻成功");
		return "redirect:"+Global.getAdminPath()+"/media/hkMediaNews/?repage";
	}

}